<blockquote class="layui-elem-quote">添加管理员</blockquote>

<!--表单-->

<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
            <input class="layui-input" type="text" name="username" required placeholder="请输入管理员用户名">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input class="layui-input" type="number" name="password" required placeholder="请输入密码">
        </div>
    </div>
    <div class="layui-form-item">
    <label class="layui-form-label">电话</label>
    <div class="layui-input-inline">
        <input class="layui-input" type="text" name="phone" required placeholder="请输入电话">
    </div>
    </div>
    <div class="layui-form-item">
    <label class="layui-form-label">邮箱</label>
    <div class="layui-input-inline">
        <input class="layui-input" type="text" name="email" required placeholder="请输入邮箱">
    </div>
    </div>
    <div class="layui-form-item">
    <label class="layui-form-label">真实姓名</label>
    <div class="layui-input-inline">
        <input class="layui-input" type="text" name="realname" required placeholder="请输入真实姓名">
    </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
                  <button type="submit" lay-submit lay-filter="admin-add"
                    class="layui-btn">提交</button>
        </div>
    </div>




</form>

<script>
    layui.use(['form'],function (){
        var form = layui.form,
            $ = layui.$;

        //表单提交
        form.on('submit(admin-add)',function (data){
            //ajax将表单的数据提交到服务器
            //添加的url   /api/admin  post请求

            $.ajax({
                url:'/api/admin/add',
                data:JSON.stringify(data.field),
                contentType:'application/json; charset=utg-8',
                type: 'post',
                dataType: 'json',

                success: function (result){
                    if (result.code ==0){
                        layer.msg("添加成功");

                    }else {
                        layer.msg(result.msg);
                    }
                },
                error: function (){
                    layer.msg("请求失败");
                }
            });
            //阻止表单的自动提交
            return false;
        });
    });

</script>